package com.huaxindata.im.main.chat.Helper;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.baidu.mapapi.model.LatLng;
import com.huaxin.common.message.BaiduMapActivity;
import com.huaxindata.im.main.chat.ConversationManager;
import com.huaxindata.im.main.chat.adapter.ChatAdapter;
import com.huaxindata.im.main.manager.UserManager;
import com.huaxindata.im.model.db.MessageStore;
import com.huaxindata.im.model.entity.ChatLocationMessageBody;
import com.huaxindata.im.model.entity.ChatMessage;
import com.huaxindata.im.model.entity.ChatMessageBody;

/**
 * Created by Administrator on 2017/9/26.
 * 一个负责处理位置消息ui的帮助类
 */

public class LocationHelper {
    public static void sendLocationMsg(double latitude, double longitude, String locationAddress, final ChatAdapter adapter) {
        ChatMessage message=new ChatMessage(ChatMessageBody.Type.LOCATION,UserManager.getClientId(),adapter.toChat.getClientId(),System.currentTimeMillis());
        message.messageBody=new ChatLocationMessageBody(latitude,longitude,locationAddress);
        message.messageState = ChatMessage.Status.DOING;
        message.read=1;//自己发的消息置为已读
        ConversationManager.sendMessage(message);
        adapter.addMessage(message);
        adapter.refreshToLast();
        MessageStore.insert(message);//将消息存入数据库
    }
    public static void handleLocationMsg(Context context, ChatAdapter adapter, ChatMessage message, ChatAdapter.ViewHolder viewHolder){
        ChatLocationMessageBody locationBody= (ChatLocationMessageBody) message.getMessageBody();
       // HxLocationMessage.LocationBody locationBody = message.getLocationBody();
        viewHolder.mLocationTv.setText(locationBody.addrInfo);
        viewHolder.mLocationImgv.setOnClickListener(new MapClickListener(context, locationBody.latlng, locationBody.addrInfo));
    }
    /**
     * 点击地图消息listener
     */
    private static class MapClickListener implements View.OnClickListener {

        LatLng location;
        String address;
        Context context;

        public MapClickListener(Context context, LatLng loc, String address) {
            location = loc;
            this.address = address;
            this.context = context;
        }

        @Override
        public void onClick(View v) {
            Intent intent;
            intent = new Intent(context, BaiduMapActivity.class);
            intent.putExtra(BaiduMapActivity.LAT, location.latitude);
            intent.putExtra(BaiduMapActivity.LNG, location.longitude);
            intent.putExtra(BaiduMapActivity.ADR, address);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }

    }
}
